// ==================================================
// 	整个程序的入口文件
// 	20180803
//	刘剑
//	563084506@qq.com
// ==================================================

let init = require("./init.js");
let vsql = require('./templates/visit_sql')
let h    = require('./templates/data_handler/a_tables.js')
let fs 	 = require('fs');
let path 	 = require('path');
let ejs  = require('ejs');

global.path=path;



let script_path = path.resolve(__dirname,"./templates/data_handler");
let mapper_path = path.resolve(__dirname,"./templates/mapper_handler");
let paths  = fs.readdirSync(script_path);
let mappers  = fs.readdirSync(mapper_path);

let handler = paths
				.map(item=>path.resolve(script_path,item))
				.map(item=>require(item))

let mapper_handler = mappers
				.map(item=>path.resolve(mapper_path,item))
				.map(item=>require(item))

module.exports = function (yo) {
	new Promise((resolve,reject)=>{
		
		vsql().then(data=>{
			var r = data;
			handler.forEach(h=>r=h(r));//获取最后处理完的数据，包括所有表格的所有字段
			if(false){
				yo.fs.writeJSON("./aaa.json", r);
				yo.fs.commit(()=>{
							console.log("ok!");
						});
			}
			if(true){//生成实体类

				Object.keys(r).map(item=>r[item]).forEach(table=>{
					fs.readFile(path.resolve(__dirname,'templates','entity.ejs'),(err,data)=>{
						if(err)throw err;
						var entry_path_real = yo.destinationPath(props.entityPath,under2Pascl(table.fields[0].TABLE_NAME)+".java");
						var content = ejs.render(data.toString(),{tb:table,dirname:path.resolve(__dirname,"templates")});
						yo.fs.write(entry_path_real, content);
						yo.fs.commit(()=>{
							console.log(entry_path_real+"ok!");
						});
					})
				});
			}

			if(true){//生成Mapper
				
				mapper_handler.forEach(h=>r=h(r));//获取最后处理完的数据，包括所有表格的所有字段
				Object.keys(r).map(item=>r[item]).forEach(table=>{
					fs.readFile(path.resolve(__dirname,'templates','mapper_inteface.ejs'),(err,data)=>{
						if(err)throw err;
						var mapper_path_real = yo.destinationPath(props.mapperClassPath,table.className+"Mapper.java");
						var content = ejs.render(data.toString(),{tb:table,dirname:path.resolve(__dirname,"templates")});
						yo.fs.write(mapper_path_real, content);
						yo.fs.commit(()=>{
							console.log(mapper_path_real+"ok!");
						});
					})
				});

				Object.keys(r).map(item=>r[item]).forEach(table=>{
					fs.readFile(path.resolve(__dirname,'templates','mapper_xml.ejs'),(err,data)=>{
						if(err)throw err;
						var mapper_path_real = yo.destinationPath(props.mapperXMLPath,table.className+"Mapper.xml");
						var content = ejs.render(data.toString(),{tb:table,dirname:path.resolve(__dirname,"templates")});
						yo.fs.write(mapper_path_real, content);
						yo.fs.commit(()=>{
							console.log(mapper_path_real+"ok!");
						});
					})
				});
			}

			if(props.servicePath){//生成Service
				
				Object.keys(r).map(item=>r[item]).forEach(table=>{
					fs.readFile(path.resolve(__dirname,'templates','service.ejs'),(err,data)=>{
						if(err)throw err;
						var service_path_real = yo.destinationPath(props.servicePath,table.className+"Service.java");
						var content = ejs.render(data.toString(),{tb:table,dirname:path.resolve(__dirname,"templates")});
						yo.fs.write(service_path_real, content);
						yo.fs.commit(()=>{
							console.log(service_path_real+"ok!");
						});
					})
				});

				
			}

			if(props.controllerPath){//生成Controller
				
				Object.keys(r).map(item=>r[item]).forEach(table=>{
					fs.readFile(path.resolve(__dirname,'templates','controller.ejs'),(err,data)=>{
						if(err)throw err;
						var service_path_real = yo.destinationPath(props.controllerPath,table.className+"Controller.java");
						var content = ejs.render(data.toString(),{tb:table,dirname:path.resolve(__dirname,"templates")});
						yo.fs.write(service_path_real, content);
						yo.fs.commit(()=>{
							console.log(service_path_real+"ok!");
						});
					})
				});

				
			}
			
			
			
		});
	});
}